{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ab8830d-edbf-4a1f-955c-27bcad1f1aec",
   "metadata": {},
   "source": [
    "# Exercise: Fixing Data in Pandas DataFrame\n",
    "\n",
    "In this exercise, you're given a small DataFrame of data that looks like it should be numeric. But when it's imported into Pandas, and you look at the `info()` for that DF, you notice it's actually been imported as a string! Oh no, with your data as strings, you can't do any data analysis. You'll practice how to fix that data to make it numeric and save the day."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "cf6b6360-db0d-4787-8c08-31e8e711b1cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "17c6caa0-cf1c-408c-9fa0-48ecadd85193",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'Price': ['USD 10', 'USD 20', 'USD 30'],\n",
    "    'Revenue': ['+100', '-10', '+500'],\n",
    "    'Quantity': ['2', '4', '6']\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54527fd8-fdc7-486f-9538-043cb426260e",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1dce4e09-d32e-410c-94fe-ab86cf37524f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3 entries, 0 to 2\n",
      "Data columns (total 3 columns):\n",
      " #   Column    Non-Null Count  Dtype \n",
      "---  ------    --------------  ----- \n",
      " 0   Price     3 non-null      object\n",
      " 1   Revenue   3 non-null      object\n",
      " 2   Quantity  3 non-null      object\n",
      "dtypes: object(3)\n",
      "memory usage: 204.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a6849d04-79cc-4a88-9874-ba920772cf9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Quantity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>USD 10</td>\n",
       "      <td>+100</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>USD 20</td>\n",
       "      <td>-10</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>USD 30</td>\n",
       "      <td>+500</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    Price Revenue Quantity\n",
       "0  USD 10    +100        2\n",
       "1  USD 20     -10        4\n",
       "2  USD 30    +500        6"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cfc48478-6fa2-4a14-98da-d75af27fc8a7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# your task: Fix the above DataFrame so that each column has numeric data (when you look at .info(), all columns should be a numeric type)\n",
    "\n",
    "df['Price'] = df['Price'].replace({'USD ': ''}, regex=True).astype(float)\n",
    "df['Revenue'] = df['Revenue'].replace({r'\\+': ''}, regex=True).astype(float)\n",
    "df['Quantity'] = df['Quantity'].astype(float)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2c1e84cd-f3f2-4503-be78-8d9dbff4fe53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Price</th>\n",
       "      <th>Revenue</th>\n",
       "      <th>Quantity</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10.0</td>\n",
       "      <td>100.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20.0</td>\n",
       "      <td>-10.0</td>\n",
       "      <td>4.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>30.0</td>\n",
       "      <td>500.0</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Price  Revenue  Quantity\n",
       "0   10.0    100.0       2.0\n",
       "1   20.0    -10.0       4.0\n",
       "2   30.0    500.0       6.0"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1d02d5d6-b04c-4f2f-a71b-76549bbc897a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 3 entries, 0 to 2\n",
      "Data columns (total 3 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   Price     3 non-null      float64\n",
      " 1   Revenue   3 non-null      float64\n",
      " 2   Quantity  3 non-null      float64\n",
      "dtypes: float64(3)\n",
      "memory usage: 204.0 bytes\n"
     ]
    }
   ],
   "source": [
    "df.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbdef80b-914f-4fc9-a1ec-dae7fc93c5de",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
